package org.example.service.impl;

import cn.hutool.jwt.JWTUtil;
import org.example.domain.CarClientPlan;
import org.example.domain.CarPlan;
import org.example.mapper.CarClientMapper;
import org.example.mapper.CarClientPlanMapper;
import org.example.mapper.CarPlanMapper;
import org.example.service.CarClientPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class CarClientPlanServiceImpl implements CarClientPlanService {
    @Autowired
    CarPlanMapper carPlanMapper;
    @Autowired
    CarClientPlanMapper carClientPlanMapper;
    @Autowired
    CarClientMapper carClientMapper;
    @Autowired
    HttpServletRequest request;
    @Override
    @Transactional
    public boolean saveClientPlan(CarPlan carPlan) {
        // 保存客户计划
        carPlanMapper.savePlan(carPlan);
        // 获取当前客户ID
        String token = request.getHeader("token");
        Integer clientId = (Integer) JWTUtil.parseToken(token).getPayload("clientId");
        System.err.println(clientId);
        // 保存客户计划关联
//        CarClientPlan carClientPlan = new CarClientPlan();
//        carClientPlan.setClientId(clientId);
//        carClientPlan.setPlanId(carPlan.getPlanId());
//        carClientPlanMapper.saveClientPlan(carClientPlan);
        // 更新客户状态（假设这个是更新客户状态的方法）
        carClientMapper.updatePlan(clientId);
        return true;
    }

    @Override
    public void QiangDan(CarClientPlan carClientPlan) {
        carClientPlanMapper.QiangDan(carClientPlan);
    }

    @Override
    public List<CarPlan> selectByPlanId(Integer clientId) {
        return carPlanMapper.selectByPlanId(clientId);
    }
}
